/**
 * Copyright (c) 2012 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.android.wordbattle.paint.bogglesimple;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.view.View;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.WordRasterContext;
import chabernac.android.wordbattle.WordRasterGameMediator;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.paint.iDrawable;

public class SimpleBoggleLetterDrawable extends iDrawable {
  private WordBattleView myWordBattleView;
  private final Letter myLetter;
  private final Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);

  public SimpleBoggleLetterDrawable( Letter aLetter ) {
    myLetter = aLetter;
  }

  @Override
  public void paint( Canvas aCanvas, View aView ) {
    myWordBattleView = (WordBattleView)aView;
    int theX = WordBattleView.BORDER_SIZE + (int)(myLetter.getColumn() * myWordBattleView.getWidthPerColumn());
    int theY = WordBattleView.BORDER_SIZE + (int)(myLetter.getRow() * myWordBattleView.getHeightPerRow());
    
    int theBorder = (int)myWordBattleView.getWidthPerColumn() / 20;
    int theRadius = (int)myWordBattleView.getWidthPerColumn() / 20;
    
    float theIncreaseOnSelect = myWordBattleView.getWidthPerColumn() / 8;
//    float theIncreaseOnSelect = theBorder * 0.9f;

//    myPaint.setColor( Color.BLUE );
//    myPaint.setStyle( Paint.Style.STROKE );
//    myPaint.setColor( Color.BLACK );
//    aCanvas.drawRect( theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getWidthPerColumn(), myPaint );
    
//    myPaint.setStyle( Paint.Style.FILL );
//    myPaint.setShader(new LinearGradient(theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getWidthPerColumn(), Color.WHITE, Color.GRAY, Shader.TileMode.MIRROR));

    RectF theRect = new RectF(theX + theBorder, theY + theBorder, theX + theBorder + myWordBattleView.getWidthPerColumn() - 2 * theBorder, theY + theBorder + myWordBattleView.getHeightPerRow() - 2 * theBorder);
    myPaint.setStyle( Paint.Style.FILL );
//    myPaint.setShadowLayer(1, 2, 2, Color.BLUE);
    if(myLetter.isSelected()){
      myPaint.setShader(new LinearGradient(theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getHeightPerRow(), Color.argb(255,100,255,255), Color.argb(255,100,100,200), Shader.TileMode.MIRROR));
      if(isIncreaseLetter()){
        theRect = new RectF(theRect.left - theIncreaseOnSelect, theRect.top - theIncreaseOnSelect, theRect.right + theIncreaseOnSelect, theRect.bottom + theIncreaseOnSelect);
      }
    } else {
      myPaint.setShader(new LinearGradient(theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getHeightPerRow(), Color.argb(255,255,255,255), Color.argb(255,200,200,200), Shader.TileMode.MIRROR));
    }
//    myPaint.setShader(new LinearGradient(theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getHeightPerRow(), Color.argb(255,255,255,255), Color.argb(255,200,200,200), Shader.TileMode.MIRROR));
    aCanvas.drawRoundRect( theRect, theRadius, theRadius, myPaint );

    
    myPaint.setShader(new LinearGradient(theX, theY, theX + myWordBattleView.getWidthPerColumn(), theY + myWordBattleView.getHeightPerRow(), Color.argb(255,100,100,100), Color.argb(255,0,0,0), Shader.TileMode.MIRROR));
    myPaint.setStyle( Paint.Style.STROKE);
    myPaint.setShader(null);
    if(isHighlighLetter()){
      myPaint.setStrokeWidth(3);
      myPaint.setColor(Color.RED);
    } else if(myLetter.isSelected()){
      myPaint.setStrokeWidth(3);
      myPaint.setColor(Color.BLACK);
    } else {
      myPaint.setStrokeWidth(1);
      myPaint.setColor(Color.BLACK);
    }
    
    aCanvas.drawRoundRect( theRect, theRadius, theRadius, myPaint );
    
    
//    myPaint.setShader(new LinearGradient(theX + 5, theY + 5, theX + myWordBattleView.getWidthPerColumn() - 10, theY + myWordBattleView.getWidthPerColumn() - 10, Color.WHITE, Color.GRAY, Shader.TileMode.MIRROR));
//    myPaint.setStyle( Paint.Style.FILL_AND_STROKE );
//    myPaint.setColor( Color.GRAY );
//    
//    aCanvas.drawCircle( theX + myWordBattleView.getWidthPerColumn() / 2, theY + myWordBattleView.getWidthPerColumn() / 2, (float)(myWordBattleView.getWidthPerColumn() * 0.45), myPaint );
    

    float theMinSize = (float)(Math.min(theRect.width(), theRect.height()) ) * 0.61f;
    if(myLetter.isSelected()) theMinSize *= 1.5;
    myPaint.setTextSize(theMinSize);
    
    Rect theFontBounds = new Rect();
    myPaint.getTextBounds(myLetter.getLetter(), 0 , 1 , theFontBounds);
    
    float theFontWidth = theFontBounds.width();
    float theFontHeight = theFontBounds.height();
    
    float theXOffset = (myWordBattleView.getWidthPerColumn() - theFontWidth) / 2F;
    float theYOffset = (myWordBattleView.getWidthPerColumn() - theFontHeight) / 2F;
    
    float theXL = theX + theXOffset;
    float theYL = theY +  theYOffset + theFontHeight; 
    
    myPaint.setShader(null);
    myPaint.setStyle( Paint.Style.FILL );
    myPaint.setColor(Color.BLACK);
    myPaint.setFakeBoldText( true );
//    myPaint.setMaskFilter( new BlurMaskFilter( 3, BlurMaskFilter.Blur.NORMAL ) );
    
    aCanvas.drawText(myLetter.getLetter(), theXL - theFontBounds.left, theYL - theFontBounds.bottom , myPaint);
    
    myPaint.setAlpha( 255 );
    myPaint.setMaskFilter( null );
  }
  
  private boolean isHighlighLetter(){
    WordRasterGameMediator theMediator = WordRasterContext.getInstance().getMediator();
    
    if(!theMediator.getRenderingHints().isShowLastSelectedLetter()) return false;
    if(theMediator.getWordRaster().getLastFoundWord() == null) return false;
    if(theMediator.getWordRaster().getLastFoundWord().getLastLetter() == myLetter) return true;
    return false;
  }
  
  private boolean isIncreaseLetter(){
    WordRasterGameMediator theMediator = WordRasterContext.getInstance().getMediator();
    
    List<Letter> theLetters = theMediator.getWordRaster().getSelectedLetters();
    if(!theLetters.isEmpty() && theLetters.get(theLetters.size() - 1) == myLetter){
      return true;
    }
    return false;
  }
  
  public int getLayer(){
    if(isIncreaseLetter()){
      return 0;
    }
    return 1;
  }

}
